import { fromJS } from 'immutable'
import * as constants from './constants'

/* immutable.js库  将数据不可改变  */

const defaultState = fromJS({
  topicList: [],
  articleList: [],
  recommendList: [],
  writerList: [],
  articlePage: 1
})

const changeHomeData = (state, action) => {
  return state.merge({
    topicList: fromJS(action.topicList),
    articleList: fromJS(action.articleList),
    recommendList: fromJS(action.recommendList),
    writerList: fromJS(action.writerList)
  })
}

const changeHomeList = (state, action) => {
  return state.merge({
    articleList: state.get('articleList').concat(fromJS(action.list)),
    articlePage: action.nextPage
  })
}

export default (state = defaultState, action) => {
  /* 
      immutable对象set方法，会结合之前immutable对象的值
      和设置的值，返回一个全新的对象
    */
  switch (action.type) {
    case constants.CHANGE_HOME_DATA:
      return changeHomeData(state, action)
    case constants.CHANGE_HOME_LIST:
      return changeHomeList(state, action)
    default:
      return state
  }
}
